package com.t174.car.controller;

import com.t174.car.dao.PermissionBeanDao;
import com.t174.car.dao.RoleDao;
import com.t174.car.dao.RolePermissionDao;
import com.t174.car.entity.Permission;
import com.t174.car.entity.Role;
import com.t174.car.pojo.PermissionBean;
import com.t174.car.pojo.StateBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Role")
@Scope("prototype")//默认到单例模式
public class RoleController {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionBeanDao permissionBeanDao;
    @Autowired
    private RolePermissionDao rolePermissionDao;

    @RequestMapping("/queryRole")//查询全部
    @ResponseBody
    public List<Role> queryRole(){
        return roleDao.query();
    }
    @RequestMapping("/addsx")
    @ResponseBody
    public int addrole(Role role){
        return  roleDao.AddRole(role);
    }
    @RequestMapping("/deleteRole")
    @ResponseBody
    public int del(String ids){
        String[] ss = ids.split("-");
        Integer[] myids = new Integer[ss.length] ;
        for(int i=0;i<ss.length;i++){
            System.out.println(ss[i]);
            myids[i] = Integer.parseInt(ss[i]) ;
        }
        return roleDao.delRole(myids);
    }
    @RequestMapping("/fenpei")
    @ResponseBody
    public List<PermissionBean> fenpei(int oid){
        List<Permission> rlist = permissionBeanDao.queryByRole(oid);
        List<PermissionBean> list = permissionBeanDao.query(0);
        for (PermissionBean p: list) {
            p.setNodes(permissionBeanDao.query(p.getId()));
        }
        //回显当前角色拥有的所有权限
        for(PermissionBean per : list){
            StateBean stateBean = new StateBean() ;
            for(Permission permission : rlist){
                if(per.getId() == permission.getId()){
                    stateBean.setChecked(true);
                }
            }
            per.setState(stateBean);
            //所有的子节点
            List<PermissionBean> listSon = per.getNodes() ;
            for(PermissionBean permission1 : listSon){
                StateBean stateBean1 = new StateBean() ;
                for(Permission permission11 : rlist){
                    if(permission1.getId() == permission11.getId()){
                        stateBean.setChecked(true);
                        stateBean1.setChecked(true);
                    }
                }
                permission1.setState(stateBean1);
            }
        }
        return list;
    }


    @RequestMapping("/grantPermission")
    @ResponseBody
    public Map<String,Object> grantPermission(int roleid, String pmid){
        Map<String,Object> map = new HashMap<>() ;
        try {
            String[] ss = pmid.split("-") ;
            Integer[] perids = new Integer[ss.length] ;
            for(int i=0;i<perids.length;i++){
                perids[i] = Integer.parseInt(ss[i]) ;
            }
            //删除角色对应的权限
            rolePermissionDao.delRole(roleid);
            //增加用户选 择的权限
            for(int permissionid : perids) {
                rolePermissionDao.seve(roleid, permissionid);
            }
            map.put("msg","1") ;
        } catch (NumberFormatException e) {
            map.put("msg","2") ;
            e.printStackTrace();
        }


        return map ;
    }
}
